<% // Copyright 2008 Carnegie Mellon University
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, 
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License. %>

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page pageEncoding="UTF-8" %> 
<%@page import="java.util.Properties"%>
<%@page import="edu.cmu.lti.sepia.data.resource.ResourceManager"%>

<% 
response.setHeader("progma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("UTF8");

ResourceManager.applicationInit(application);
boolean debugMode = ResourceManager.debugMode;

//Defined in the property file ... this is null if setup is never done
String projectName = (String)application.getAttribute("projectName");

boolean error = request.getParameter("state")!=null;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta name="robots" content="noindex,nofollow"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    <meta http-equiv="Cache-Control" content="no-cache"/>

    <% if (!ResourceManager.initializedAlready()) { //projectName==null ||  %>
    <meta http-equiv="Refresh" content="0; URL=<%=request.getContextPath()%>/setup">
    <% } %>

    <title>Authentication and Authorization</title>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/external/jquery.js"></script>
    <script type="text/javascript"><!-- jQuery.noConflict(); //--></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/js/external/prototype.js"></script>
    <script type="text/javascript">
      <%
       //Must be followed by shared.js ... originally defined in resource.js.jsp
       Properties p = ResourceManager.readBundledConfig();
       String sepiaVersion = "v"+p.getProperty("version","");
       %>      var sepiaVersion = "<%=sepiaVersion%>";
    </script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/js/shared.js"></script>
    <script type="text/javascript">
    jQuery.fn.center = function () {
      this.css("position","absolute");
      this.css("top", ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
      this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
      return this;
    };
    jQuery(document).ready( function() {
      jQuery("#box").center();
    });
    jQuery(window).resize( function() {
      jQuery("#box").center();
    });
    </script>
    <style type="text/css">
    	.floatingbox { <%=debugMode?"border:2px solid #f00":"border:1px solid #999" %> }
    </style>
  </head>
<body>
&nbsp;
<!--help-->

<div id="box">
<br><br>

<div align="center"><h2>SEPIA<%=(projectName!=null?(" for "+projectName.toUpperCase()):"")%></h2></div>

<br>

<!-- Warning for users who does not allow JavaScript execution -->
<noscript>
  <div align="center">
    <table style="font-weight:bold;color:#ff0000;border:1px solid #ff0000">
      <tr>
	<td>JavaScript is disabled or not supported in your web browser.<br/>Please use Internet Explorer 7 or Firefox 2 or above, and enable JavaScript.<br/><br/>Thanks.</td>
      </tr>
    </table>
  </div>
  <br/>
</noscript>

<div align="center">
<%=debugMode?"<strong style=\"color:#f00\">You are currently in debug mode</strong>":"" %>
<table border="0" cellpadding="10" cellspacing="0" class="floatingbox">
  <tr>
    <td align="center">

		<% if (error) {
			out.println("<strong style=\"color:#f00;\">");
			out.println("Authorization failed! Make sure to <br>enter correct username and password</strong>");
		}%>
		    
		<form method="post" action="<%= response.encodeURL("j_security_check")%>" name="auth">
		<%=debugMode?"<!--":"" %> 
		<table width="100%">
		  <tr>
		    <td width="80" align="right">Username: </td>
		    <td align="left"><input type="text" name="j_username" id="j_username" style="width:120px" onchange="document.auth.j_username.value=this.value"></td>
		  </tr>
		  <tr>
		    <td align="right">Password: </td>
		    <td align="left"><input type="password" name="j_password" id="j_password" style="width:120px" onchange="document.auth.j_password.value=this.value"></td>
		  </tr>
		</table>
		<%=debugMode?"-->":"" %>
		<%
		if (debugMode) { 
			out.println("<input type=\"hidden\" name=\"j_username\" id=\"j_username\">");
			out.println("<input type=\"hidden\" name=\"j_password\" id=\"j_password\">");
			out.println("<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;LOG IN AS SUPERUSER&nbsp;&nbsp;&nbsp;&nbsp;\" name=\"fakeSubmit2\" onclick=\"$('j_username').value='admin';$('j_password').value='admin';\">");
		} else {
			out.println("<input type=\"submit\" value=\"&nbsp;&nbsp;&nbsp;&nbsp;LOG IN&nbsp;&nbsp;&nbsp;&nbsp;\" name=\"fakeSubmit\">");
		}
		%>
		</form>
		</td></tr></table>

		<table width="500" align="center"><tr><td style="font-size:12px">
		<br><br>
    </td></tr></table>
  </div>

<br>
</div>

<!-- FOOTER -->
<div id="footer"></div>

</body>
</html>
